package com.wy.web.old_contact;

import com.wy.pojo.Contact;
import com.wy.service.ContactService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/contact/addContent")
public class AddContactServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try{
            Map<String, String[]> map = req.getParameterMap();
            Contact contact = new Contact();
            BeanUtils.populate(contact,map);
            System.out.println("请求携带的数据"+contact);

            ContactService contactService = new ContactService();
            boolean flag = contactService.addContact(contact);
            if(flag){
                resp.sendRedirect(req.getContextPath()+"/contact/findAll");
            }else{
                req.setAttribute("msg","当前功能正在维护");
                req.getRequestDispatcher("/contact/add.jsp");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
